package com.example.statistics.ui.product

import android.os.Bundle
import com.example.statistics.R
import com.example.statistics.bean.ProductItemBean
import com.example.statistics.common.BaseActivity
import com.example.statistics.ex.format
import com.example.statistics.ex.parse
import com.example.statistics.ex.timePicker
import com.example.statistics.net.UrlConfig
import com.example.statistics.net.callback.UploadDialogHttpCallback
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityresult.ActivityResult
import com.jhj.httplibrary.HttpCall
import com.jhj.httplibrary.model.HttpParams
import kotlinx.android.synthetic.main.activity_product_detail_edit.*
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.toast
import java.io.File

class ProductDetailEditActivity : BaseActivity() {
    override val title: String
        get() = intent.getStringExtra(Constants.TITLE).orEmpty()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_product_detail_edit)
        val productInfoId = intent.getIntExtra(Constants.ID, -1)
        val data = intent.getSerializableExtra(Constants.DATA) as ProductItemBean?
        if (productInfoId == -1) {
            finish()
            return
        }
        data?.let {
            il_date.contentText = it.productDate
            il_batch.contentText = it.productBatch
            il_remark.contentText = it.remark
            layout_image.setImageList(it.formatPathList)
        }
        var date = il_date.contentText.parse()
        il_date.onSelector {
            timePicker(date) {
                il_date.contentText = it.format()
                date = it
            }
        }

        layout_commit.onClick {
            val params = HttpParams()
            val url = if (data != null) {
                val netFileList = layout_image.netFileList()
                if (!netFileList.isNullOrEmpty()) {
                    val str = netFileList.joinToString(",")
                    params.put("netImageStr" to str)
                }
                params.put("id" to data.id)
                UrlConfig.PRODUCT_DETAIL_UPDATE
            } else {
                UrlConfig.PRODUCT_DETAIL_ADD
            }
            if (layout_image.imageList().size == 0) {
                toast("${title}图片不能为空")
                return@onClick
            }

            HttpCall.post(url)
                    .addFiles("images[]", layout_image.localFileList().map { File(it) })
                    .addParams(params)
                    .addParams(
                            "productName" to title,
                            "productDate" to il_date.contentText,
                            "productBatch" to il_batch.contentText,
                            "remark" to il_remark.contentText,
                            "productInfoId" to productInfoId
                    )
                    .enqueue(object : UploadDialogHttpCallback<ProductItemBean>(this@ProductDetailEditActivity) {
                        override fun onSuccess(data: ProductItemBean?, resultType: ResultType) {
                            if (data == null) {
                                toast("上传失败")
                                return
                            }
                            ActivityResult.with(this@ProductDetailEditActivity)
                                    .putSerializable(Constants.DATA, data)
                                    .finish()
                        }
                    })
        }

    }
}